package com.xjh.him.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.xjh.him.R;
import com.xjh.him.dto.friend.FriendDTO;
import com.xjh.him.utils.Utils;
import com.xuexiang.xui.utils.DrawableUtils;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;
import com.xuexiang.xutil.display.DensityUtils;

import org.xjh.tool.Checks;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ChatGroupCreateAdapter extends BaseExpandableListAdapter {
    private Activity mActivity;
    private List<String> characterList;
    private Map<String, List<FriendDTO>> dataMap;
    private List<FriendDTO> selectList;

    private ExpandableListView.OnGroupClickListener groupClickListener;

    public ChatGroupCreateAdapter(Activity activity, Map<String, List<FriendDTO>> map) {
        mActivity = activity;
        characterList = new ArrayList<>(map.keySet());
        dataMap = map;
        selectList = new ArrayList<>();
    }


    @Override
    public int getGroupCount() {
        return characterList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return dataMap.get(characterList.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return characterList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return dataMap.get(characterList.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
        ChatGroupCreateAdapter.GroupViewHolder groupViewHolder;
        if (view == null) {
            view = LayoutInflater.from(mActivity).inflate(R.layout.adapter_group_item, parent, false);
            groupViewHolder = new ChatGroupCreateAdapter.GroupViewHolder();
            groupViewHolder.stvListGroup = view.findViewById(R.id.stv_list_group);
            view.setTag(groupViewHolder);
        }else {
            groupViewHolder = (ChatGroupCreateAdapter.GroupViewHolder) view.getTag();
        }
        if (isExpanded) {
            groupViewHolder.getStvListGroup().setLeftIcon(R.drawable.ic_arrow_down);
        }else {
            groupViewHolder.getStvListGroup().setLeftIcon(R.drawable.ic_arrow_right);
        }
        groupViewHolder.getStvListGroup().setLeftString(characterList.get(groupPosition));

        View finalView = view;
        groupViewHolder.getStvListGroup().setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                groupClickListener.onGroupClick(
                        mActivity.findViewById(R.id.elv_friend_list),
                        finalView, groupPosition, getGroupId(groupPosition));
            }
        });
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        ChatGroupCreateAdapter.ChildViewHolder childViewHolder;
        if (view == null) {
            view = LayoutInflater.from(mActivity).inflate(R.layout.adapter_chat_group_create_item, parent, false);
            childViewHolder = new ChatGroupCreateAdapter.ChildViewHolder();
            childViewHolder.stvListItem = view.findViewById(R.id.stv_list_item);
            childViewHolder.cbSelectCheck = view.findViewById(R.id.cb_select_check);
            view.setTag(childViewHolder);
        }else {
            childViewHolder = (ChatGroupCreateAdapter.ChildViewHolder) view.getTag();
        }
        SuperTextView stvListItem = childViewHolder.getStvListItem();
        FriendDTO friendDTO = (FriendDTO) getChild(groupPosition, childPosition);
        if (Checks.isEmpty(friendDTO.getAvatarUrl())) {
            BitmapDrawable bitmapDrawable = DrawableUtils.createCircleDrawableWithText(
                    mActivity.getResources(),
                    DensityUtils.dip2px(38),
                    ContextCompat.getColor(mActivity, R.color.app_color_theme_7),
                    friendDTO.getNickname().substring(0, 1),
                    com.xuexiang.xui.utils.DensityUtils.sp2px(mActivity, 20),
                    Color.WHITE);
            stvListItem.setLeftIcon(bitmapDrawable);
        }else {
            ImageView leftIconIV = stvListItem.getLeftIconIV();
            Utils.setImageViewParams(leftIconIV, 40, 40);
            Glide.with(mActivity)
                    .load(friendDTO.getAvatarUrl())
                    .into(leftIconIV);
        }
        stvListItem.setLeftTopString(friendDTO.getNickname());
        if (selectList.contains(friendDTO)) {
            childViewHolder.cbSelectCheck.setChecked(true);
        }else {
            childViewHolder.cbSelectCheck.setChecked(false);
        }

        childViewHolder.cbSelectCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (childViewHolder.cbSelectCheck.isChecked()) {
                    selectList.add(friendDTO);
                }else {
                    selectList.remove(friendDTO);
                }

                Button button = mActivity.findViewById(R.id.btn_create);
                if (selectList.size() > 0) {
                    button.setEnabled(true);
                }else {
                    button.setEnabled(false);
                }
            }
        });
        stvListItem.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                childViewHolder.getCbSelectCheck().performClick();
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public void setGroupClickListener(ExpandableListView.OnGroupClickListener groupClickListener) {
        this.groupClickListener = groupClickListener;
    }

    public List<FriendDTO> getSelectList() {
        return selectList;
    }

    private static class GroupViewHolder {
        public SuperTextView stvListGroup;

        public SuperTextView getStvListGroup() {
            return stvListGroup;
        }

        public void setStvListGroup(SuperTextView stvListGroup) {
            this.stvListGroup = stvListGroup;
        }
    }

    private static class ChildViewHolder {
        public SuperTextView stvListItem;
        public CheckBox cbSelectCheck;

        public SuperTextView getStvListItem() {
            return stvListItem;
        }

        public void setStvListItem(SuperTextView stvListItem) {
            this.stvListItem = stvListItem;
        }

        public CheckBox getCbSelectCheck() {
            return cbSelectCheck;
        }

        public void setCbSelectCheck(CheckBox cbSelectCheck) {
            this.cbSelectCheck = cbSelectCheck;
        }
    }
}
